package com.ray.auth.system.service.impl;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.auth.system.service.WorkService;
import com.ray.auth.system.table.entity.Work;
import com.ray.auth.system.table.entity.WorkExample;
import com.ray.auth.system.table.entity.WorkExample.Criteria;
import com.ray.auth.system.table.mapper.WorkMapper;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class WorkServiceImpl extends BaseService implements WorkService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(WorkServiceImpl.class);
	
	@Autowired
	private WorkMapper workMapper;

	/**
	 * 新增
	 */
	@Transactional
	public Result<Object> addWork(Work params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
		params.setWorkId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count=workMapper.insertSelective(params);
		if(count!=1){
			logger.error("WorkServiceImpl=>addWork is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Work queryWork(String id, LoginUser user) {
		return workMapper.selectByPrimaryKey(id);
	}

	@Transactional
	public Result<Object> updateWork(Work params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=workMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("WorkServiceImpl=>updateWork is error");
			throw new BussinessException(-1, ErrorMsg.UPDATE_ERROR);
		}
		return result;
	}
	@Transactional
	public Result<Object> deleteWork(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		Work params=new Work();
		params.setWorkId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=workMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("WorkServiceImpl=>deleteWork is error");
			throw new BussinessException(-1, ErrorMsg.DELETE_ERROR);
		}
		return result;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<Work> queryWork(Work params, PageDto<Work> page, LoginUser user) {
		WorkExample example=new WorkExample();
		Criteria criteria =  example.createCriteria();
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
			criteria.andCreateByUserEqualTo(user.getUserId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		page.setTotal(workMapper.countByExample(example));
		page.setRecords(workMapper.selectByExample(example));
		return page;
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<Work> queryMyTodoWork(Work params, PageDto<Work> page, LoginUser user) {
		WorkExample example=new WorkExample();
		Criteria criteria =  example.createCriteria();
		criteria.andTenantIdEqualTo(user.getTenantId());
		criteria.andCreateByUserEqualTo(user.getUserId());
//		criteria.andWorkEndTimeGreaterThan(new Date());
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(workMapper.countByExample(example));
		page.setRecords(workMapper.selectByExample(example));
		return page;
	}

}
